package com.forum.controller;

import com.forum.annotation.IgnoreAuth;
import com.forum.utils.R;
import com.forum.utils.ValidateCodeUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.concurrent.TimeUnit;

/**
 * @ClassNameAliSendCodeController
 * @Author Lenovo
 * @Date 2023/12/11 16:23
 */
@Api(tags = "阿里云业务公共接口")
@RestController
@RequestMapping("/sendCode")
@CrossOrigin//解决跨域问题
public class AliSendCodeController {

    @Autowired
    private RedisTemplate redisTemplate;
    //@IgnoreAuth//权限放行下同
    @IgnoreAuth
    @GetMapping("/sendMsg/{PhoneNum}")
    @ApiOperation(value = "发送短信验证码",notes = "")
    public R sendMsg(@PathVariable("PhoneNum") String PhoneNum, HttpSession session){
        if (StringUtils.isNotEmpty(PhoneNum)){
            Integer code = ValidateCodeUtils.generateValidateCode(4);
            //阿里云短信发送模版传参
            /*正式环境可开启*/
            //SMSUtils.sendMessage("岸上见考研互助平台","SMS_461305808",PhoneNum,code);
            //将生成的验证码code放入redis当中进行存储，并设置过期时间（小时）
            redisTemplate.opsForValue().set(PhoneNum,code,1L, TimeUnit.HOURS);

            //TODO 以下内容用于开发阶段进行控制台输出查看验证码
            Integer o = (Integer)redisTemplate.opsForValue().get(PhoneNum);
            System.out.println(o);
            //开发过程中开启测试使用，生产环境中注释
            //return R.ok().put("data",code);
        }
        return R.ok("短信发送成功");
    }
}
